
import cv2

# 加载级联分类器模型
faceCascade = cv2.CascadeClassifier("D:\Deep_learning\Anaconda\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml")
#cap = cv2.VideoCapture(0)  # 打开摄像头
#ret, image = cap.read()  # 获取图片
image = cv2.imread('img.jpg', 1)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  # 图像灰度化（降低运算强度）

# 人脸检测
faces = faceCascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(30, 30),)

# 在原来的彩图上画出包围框（绿色，宽度为2）
for (x, y, width, height) in faces:
    cv2.rectangle(image, (x, y), (x+width, y+height), (0, 255, 0), 2)

# 显示图片
cv2.imshow("Face", image)
cv2.waitKey(0)





